<template>
  <div class="v-table">
    <el-table :data="tableData" style="width: 100%" v-bind="$attrs" v-on="$listeners" ref="TableRef"
      v-loading="loading">
      <el-table-column v-if="(payload || {}).showSelection !== false" type="selection" width="55" align="center"
        reserve-selection> </el-table-column>
      <el-table-column v-if="(payload || {}).showIndex !== false" type="index" width="50" label="序号" align="center"
        :index="indexMethod"></el-table-column>
      <el-table-column v-for="item in tableColumn" :key="item.prop" v-bind="item">
        <template slot-scope="scope">
          <template v-if="item.slotName">
            <slot :name="item.slotName" v-bind="scope">{{ scope.row[item.prop] }}</slot>
          </template>

          <template v-else-if="item.formatter">{{ item.formatter(scope.row, scope.column, scope.row[item.prop])
            }}</template>

          <template v-else>{{ defaultValue(scope.row[item.prop]) }}</template>
        </template>
      </el-table-column>
      <template v-slot:empty><el-empty :image-size="100"></el-empty></template>
    </el-table>
    <el-pagination v-if="(payload || {}).showPagination !== false" :class="position" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="pagination.currentPage"
      :page-sizes="pagination.pageSizes || defaultPageSizes" :page-size="pagination.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="pagination.total" background>
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'VTable',
  props: {
    tableColumn: {
      type: Array,
      required: true,
      default: () => []
    },
    tableData: {
      type: Array,
      required: true,
      default: () => []
    },
    pagination: {
      type: Object,
      required: false,
      default: () => ({})
    },
    payload: {
      type: Object,
      required: false,
      default: () => ({})
    }
  },

  data() {
    return {
      loading: true,

    }
  },
  computed: {
    position() {
      return `is-${(this.payload || {}).paginationPosition || 'right'}`
    }
  },
  methods: {
    clearSelection() {
      return this.$refs.TableRef.clearSelection()
    },
    toggleRowSelection(row, selected) {
      return this.$refs.TableRef.toggleRowSelection(row, selected)
    },
    toggleAllSelection() {
      return this.$refs.TableRef.toggleAllSelection()
    },
    toggleRowExpansion(row, expanded) {
      return this.$refs.TableRef.toggleRowExpansion(row, expanded)
    },
    setCurrentRow(row) {
      return this.$refs.TableRef.setCurrentRow(row)
    },
    clearSort() {
      return this.$refs.TableRef.clearSort()
    },
    clearFilter(columnKey) {
      return this.$refs.TableRef.clearFilter(columnKey)
    },
    doLayout() {
      return this.$refs.TableRef.doLayout()
    },
    sort(prop, order) {
      return this.$refs.TableRef.doLayout(prop, order)
    },

    indexMethod(index) {
      return (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1;
    },
    defaultValue(cellValue) {
      if (cellValue === 0 || cellValue === '0') return cellValue
      return cellValue || (this.payload || {}).emptyCellValue || '--'
    },

    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.pagination.currentPage = 1 //页码变了手动回到第一页
      this.loading = true
      this.$emit('fetchTableData')

    },
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.loading = true
      this.$emit('fetchTableData')
    }
  },
  mounted() {
  },
  beforeCreate() {
    this.defaultPageSizes = [5, 10, 20, 50, 100]
  },
  watch: {
    tableData: {
      handler(newVal, oldVal) {
        this.loading = false
      },
      immediate: true

    }
  }
}
</script>

<style lang="scss" scoped>
.v-table {
  width: 100%;
  display: grid;
  grid-template-rows: auto auto;
  grid-template-columns: 1fr;
  gap: 20px 0;
}

.el-pagination {
  justify-self: start;

  &.is-left {
    justify-self: start;
  }

  &.is-center {
    justify-self: center;
  }

  &.is-right {
    justify-self: end;
  }
}
</style>